//
//  HWQQGameTypeNormalCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/6/7.
//  Copyright © 2023 94hwan. All rights reserved.
//

import UIKit

class HWQQGameTypeNormalCell: UITableViewCell {
    var rankLabel:UILabel!
    var logoImageView:UIImageView!
    var nameLabel:UILabel!
    var contentLabel:UILabel!
    var textTagCollectionView:TTGTextTagCollectionView!
    var openLabel:UILabel!
    var tagArr:[String] = []
    var indexPath:IndexPath! {
        didSet {
            rankLabel.text = "\(indexPath.row + 4)"
        }
    }
    var model:HWQQGameListModel?{
        didSet {
            
            logoImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
            nameLabel.text = model?.name
            contentLabel.text = model?.content
            
            textTagCollectionView.removeAllTags()
            tagArr.removeAll()
            for (_,txt) in (model?.biaoqian ?? []).enumerated() {
                tagArr.append(txt.first?.description ?? "")
                tagArr.append("\(txt + " ")")
            }
            
            for (idx,txt) in tagArr.enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 9*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
                normalStyle.shadowColor = .colorWithHexString(hexString: "#FFFFFF")
                normalStyle.borderColor = .clear
                normalStyle.cornerRadius = 0
                normalStyle.textAlignment = .left
                normalStyle.minWidth = 14*widthScale
                normalStyle.minHeight = 14*widthScale
                if idx == 1{
                    content.textColor = UIColor.colorWithHexString(hexString: "#6593FD")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FD9F65")
                }else if idx == 5{
                    content.textColor = UIColor.colorWithHexString(hexString: "#FF726A")
                }else if idx == 7{
                    content.textColor = UIColor.colorWithHexString(hexString: "#5AE2B0")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
        
                if idx % 2 == 0 {
                    normalStyle.cornerRadius = 7*widthScale
                    normalStyle.extraSpace = CGSize(width: 4*widthScale, height: 2*widthScale)
                    content.textColor = .white
                    normalStyle.textAlignment = .center
                }
                
                if idx == 0 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#6593FD")
                } else if idx == 2 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FD9F65")
                } else if idx == 4 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FF726A")
                } else if idx == 6 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#5AE2B0")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
            }
        }
    }
    
    var historyModel:HWQQGameHistoryListModel?{
        didSet {
            logoImageView.kf.setImage(with: URL(string: historyModel?.icon ?? ""))
            nameLabel.text = historyModel?.name
            contentLabel.text = historyModel?.content
            
            textTagCollectionView.removeAllTags()
            tagArr.removeAll()
            for (_,txt) in (historyModel?.biaoqian ?? []).enumerated() {
                tagArr.append(txt.first?.description ?? "")
                tagArr.append("\(txt + " ")")
            }
            
            for (idx,txt) in tagArr.enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 9*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
                normalStyle.shadowColor = .colorWithHexString(hexString: "#FFFFFF")
                normalStyle.borderColor = .clear
                normalStyle.cornerRadius = 0
                normalStyle.textAlignment = .left
                normalStyle.minWidth = 14*widthScale
                normalStyle.minHeight = 14*widthScale
                if idx == 1{
                    content.textColor = UIColor.colorWithHexString(hexString: "#6593FD")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FD9F65")
                }else if idx == 5{
                    content.textColor = UIColor.colorWithHexString(hexString: "#FF726A")
                }else if idx == 7{
                    content.textColor = UIColor.colorWithHexString(hexString: "#5AE2B0")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
        
                if idx % 2 == 0 {
                    normalStyle.cornerRadius = 7*widthScale
                    normalStyle.extraSpace = CGSize(width: 4*widthScale, height: 2*widthScale)
                    content.textColor = .white
                    normalStyle.textAlignment = .center
                }
                
                if idx == 0 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#6593FD")
                } else if idx == 2 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FD9F65")
                } else if idx == 4 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FF726A")
                } else if idx == 6 {
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#5AE2B0")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
            }
        }
    }
    class func cellWithTableView(_ tableView:UITableView) -> (HWQQGameTypeNormalCell) {
        let reUse:String = "HWQQGameTypeNormalCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWQQGameTypeNormalCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWQQGameTypeNormalCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        rankLabel = HWLabel.createLabel(title: "", color: "#9E9E9E", font: 14*widthScale)
        contentView.addSubview(rankLabel)
        
        logoImageView = UIImageView()
        logoImageView.backgroundColor = .clear
        logoImageView.layer.cornerRadius = 10*widthScale
        logoImageView.layer.masksToBounds = true
        logoImageView.contentMode = .scaleAspectFill
        contentView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(nameLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 9*widthScale)
        contentView.addSubview(contentLabel)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left;
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        textTagCollectionView.horizontalSpacing = 4*widthScale
        contentView.addSubview(textTagCollectionView)
        
        openLabel = HWLabel.createBoldLabel(title: "打开", color: "#FF8B00", font: 13*widthScale)
        openLabel.textAlignment = .center
        openLabel.layer.cornerRadius = 14*widthScale
        openLabel.layer.borderWidth = 1*widthScale
        openLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF8B00").cgColor
        contentView.addSubview(openLabel)
        
    }
    
    func initFrame() -> Void {
        rankLabel.snp.makeConstraints { make in
            make.left.equalTo(17*widthScale)
            make.centerY.equalTo(logoImageView)
        }
        
        logoImageView.snp.makeConstraints { make in
            make.left.equalTo(45*widthScale)
            make.top.equalTo(0).priority(.high)
            make.bottom.equalTo(-10*widthScale)
            make.width.height.equalTo(50*widthScale)
        }
            
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(8*widthScale)
            make.top.equalTo(logoImageView.snp.top).offset(2*widthScale)
            make.height.equalTo(13*widthScale)
            make.right.lessThanOrEqualTo(openLabel.snp.left).offset(-5*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(4*widthScale)
            make.height.equalTo(13*widthScale)
            make.right.lessThanOrEqualTo(openLabel.snp.left).offset(-5*widthScale)
        }
        
        openLabel.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(logoImageView)
            make.height.equalTo(28*widthScale)
            make.width.equalTo(60*widthScale)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(openLabel.snp.left)
            make.height.equalTo(20*widthScale)
            make.top.equalTo(contentLabel.snp.bottom).offset(3*widthScale)
        }

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
